package com.example.JavaWebApi.service.impl.systemImpl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.JavaWebApi.mapper.systemMapper.RoleinfoMapper;
import com.example.JavaWebApi.mapper.systemMapper.RolemapmenuMapper;
import com.example.JavaWebApi.model.entity.systemEntity.Roleinfo;
import com.example.JavaWebApi.model.entity.systemEntity.Rolemapmenu;
import com.example.JavaWebApi.model.entity.systemEntity.Usermaprole;
import com.example.JavaWebApi.model.vo.systemVo.IntegerBothBindVo;
import com.example.JavaWebApi.service.systemService.IRoleinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杨泽
 * @since 2022-06-15
 */
@Service
public class RoleinfoServiceImpl extends ServiceImpl<RoleinfoMapper, Roleinfo> implements IRoleinfoService {

    @Autowired
    private RolemapmenuMapper rolemapmenuMapper;
    @Autowired
    private UsermaproleServiceImpl usermaproleService;
    @Autowired
    private RolemapmenuServiceImpl rolemapmenuService;
    @Override
    public IPage<Roleinfo> getRoleList(int page, int limit) {
        Page<Roleinfo> pageModel=new Page<>(page,limit);
        return this.baseMapper.selectPage(pageModel,null);
    }

    @Override
    public boolean create(Roleinfo roleinfo) {
        return this.baseMapper.insert(roleinfo)>0;
    }

    @Override
    public boolean update(Roleinfo roleinfo) {
        return this.baseMapper.updateById(roleinfo)>0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(String roleId) {
        this.baseMapper.deleteById(roleId);
        UpdateWrapper<Rolemapmenu> wrapper=new UpdateWrapper<>();
        wrapper.eq("RoleID",roleId);
        rolemapmenuMapper.delete(wrapper);
        return true;
    }

    @Override
    public boolean roleForUser(IntegerBothBindVo vo) {
        List<Usermaprole> list=new ArrayList<>();
        vo.getSubIds().forEach(t->{
            Usermaprole item=new Usermaprole();
            item.setRoleID(vo.getMainId());
            item.setUserID(t);
            list.add(item);
        });
        return usermaproleService.saveBatch(list);
    }

    @Override
    public boolean cancelRoleForUser(IntegerBothBindVo vo) {
        UpdateWrapper<Usermaprole> wrapper=new UpdateWrapper<>();
        wrapper.eq("RoleId",vo.getMainId())
                .in("UserId",vo.getSubIds());
        return usermaproleService.remove(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean roleForMenu(IntegerBothBindVo vo) {
        List<Rolemapmenu> list=new ArrayList<>();
        vo.getSubIds().forEach(t->{
            Rolemapmenu item=new Rolemapmenu();
            item.setRoleId(vo.getMainId());
            item.setMenuId(t);
            list.add(item);
        });
        UpdateWrapper<Rolemapmenu> wrapper=new UpdateWrapper<>();
        wrapper.eq("RoleID",vo.getMainId());
        rolemapmenuService.remove(wrapper);
        return rolemapmenuService.saveBatch(list);
    }

}
